#include "Common.h"
#include "Utils/Util.h"
#include "Imaging/Image.h"
#include "Texture.h"
#include "Graphics/Renderer.h"
#include "Utils/Logger/Logger.h"
#include "Utils/Util.h"

using namespace RayFrame;

IMPL_BUCKET_TYPE(RayFrame, TextureBucket, Texture);

Texture* Texture::GetDefaultTexture()
{
  Texture* result = SearchTexture("DefaultTexture");
  if(result)
    return result;
  else
  {
    result = g_renderer->CreateTexture(Texture::MIPMAP, "DefaultTexture");
    result->Create(*ImageManager::InstanceRef().GetDefaultImage());
    assert(result->IsOK());
    return result;
  }
}

void Texture::Create(size_t width, size_t height, PixelFormat pf)
{
  m_width = width;
  m_height = height;
  m_format = pf;
  
  internalCreate();
}

void Texture::Create(Image &img)
{
  Create(img.GetWidth(), img.GetHeight(), img.GetPixelFormat());

  if(m_isOK)
    Upload(img);
}

Texture* Texture::CreateFromImage(Image& img)
{
  Texture* result = g_renderer->CreateTexture();
  result->Create(img);
  return result;
}

Texture* Texture::CreateFromFile(STRING filename)
{
  Image* img = ImageManager::InstanceRef().LoadFromFile("", filename);
  assert(img);
  return CreateFromImage(*img);
}